/*
 * SortedBrowseTableModel.java
 *
 * Created on 30. listopad 2006, 15:11
 *
 */

package cz.results.winter6.view.browse.table;

import cz.results.winter6.model.StartSubjekt;
import cz.results.winter6.model.StartSubjektList;
import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.table.AbstractTableModel;

public class SortedBrowseTableModel extends AbstractTableModel{
    
    protected BrowseTableModel sourceModel;
    protected int[] indexValues;
    
    public SortedBrowseTableModel(BrowseTableModel sourceModel) {
        super();
        this.sourceModel = sourceModel;
    }
    
    public void setSl(StartSubjektList sl){
        sourceModel.setSl(sl);
    }
    
    public StartSubjektList getSl(){
        return sourceModel.getSl();
    }
    
    public int getRowCount() {
        return sourceModel.getRowCount();
    }
    
    public int getColumnCount(){
        return sourceModel.getColumnCount();
    }
    
    public Object getValueAt(int row, int col){
        if(indexValues!=null){
            row = getSourceIndex(row);
        }
        return sourceModel.getValueAt(row,col);
    }
    
    public void setValueAt(Object value, int row, int col){
        if(indexValues!=null){
            row = getSourceIndex(row);
        }
        sourceModel.setValueAt(value,row,col);
    }
    
    /** vrati startovni subjekt odpovidajici radku row tabulky */
    public StartSubjekt getStartSubjektAt(int row){
        if(indexValues!=null){
            row = getSourceIndex(row);
        }
        return sourceModel.getStartSubjektAt(row);
    }
    
    public boolean isCellEditable(int row, int col){
        if(indexValues!=null){
            row = getSourceIndex(row);
        }
        return sourceModel.isCellEditable(row,col);
    }
    
    public String getColumnName(int col){
        return sourceModel.getColumnName(col);
    }
    
    public Class getColumnClass(int col){
        return sourceModel.getColumnClass(col);
    }
    
    public int getSourceIndex(int index){
        if(indexValues!=null){
            return indexValues[index];
        }
        return -1;
    }
    
    public void sortRows(int col, boolean ascending){
        SortedItemHolder holder;
        TreeSet sortedList = new TreeSet();
        int count = getRowCount();
        for (int i = 0; i < count; i++) {
            holder = new SortedItemHolder(sourceModel.getValueAt(i,col),i);
            sortedList.add(holder);
            
        }
        indexValues = new int[count];
        Iterator iterator = sortedList.iterator();
        int index = (ascending? 0:count-1);
        while (iterator.hasNext()){
            holder = (SortedItemHolder)(iterator.next());
            indexValues[index]=holder.position;
            index += (ascending? 1:-1);
        }
        refreshViews();
    }
    
    public void clearSort(){
        indexValues=null;
        refreshViews();
    }
    
    public void refreshViews(){
        fireTableDataChanged();
    }
    
    
    class SortedItemHolder implements Comparable {
        
        public final Object value;
        public final int position;
        
        public SortedItemHolder(Object value, int position){
            this.value = value;
            this.position = position;
        }
        
        public int compareTo(Object parm){
            SortedItemHolder holder = (SortedItemHolder) parm;
            Comparable comp = (Comparable) value;
            int result = comp.compareTo(holder.value);
            if(result == 0){
                result = (position < holder.position)? -1:1;
            }
            return result;
        }
        
        public int hashCode(){
            return position;
        }
        
        public boolean equals(Object comp){
            if(comp instanceof SortedItemHolder){
                SortedItemHolder other = (SortedItemHolder)comp;
                if((position == other.position) && (value==other.value)){
                    return true;
                }
            }
            return false;
        }
    }
    
    public ConfigBrowseTableModel getConfigBrowseTableModel(){
        return sourceModel.getConfigBrowseTableModel();
    }
    
    public void setConfigBrowseTableModel(ConfigBrowseTableModel configBrowseTableModel){
        sourceModel.setConfigBrowseTableModel(configBrowseTableModel);
        fireTableStructureChanged();
    }
    
}
